package com.google.gwt.user.client.rpc.core.java.util;

import java.util.UUID;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamReader;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;

/**
 * A custom serializer for our UUID class.
 * 
 * @author Ross M. Lodge
 */
public final class UUID_CustomFieldSerializer
{

    /**
     * @param streamReader
     * @param instance
     * @throws SerializationException
     */
    public static void deserialize(SerializationStreamReader streamReader, UUID instance) throws SerializationException
    {
        //Do nothing, because the only field we care about is built with instantiate
    }

    /**
     * @param streamReader
     * @return
     * @throws SerializationException
     */
    public static UUID instantiate(SerializationStreamReader streamReader) throws SerializationException
    {
        try
        {
            return UUID.fromString(streamReader.readString());
        }
        catch (Exception e)
        {
            throw new SerializationException(e.getClass() + ":" + e);
        }
    }

    /**
     * @param streamWriter
     * @param instance
     * @throws SerializationException
     */
    public static void serialize(SerializationStreamWriter streamWriter, UUID instance) throws SerializationException
    {
        streamWriter.writeString(instance.toString());
    }

}
